package com.yonyou.pmclouds.siteinspection.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.siteinspection.entity.SiteInspectionVO;
import com.yonyou.pmclouds.siteinspection.rmiitf.PatrolMaintain;
import com.yonyou.pmclouds.user.entity.UserInfo;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 巡查记录
 */
@Slf4j
@RestController
@RequestMapping({"/mobile/patrol","/patrol"})
public class PatrolController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private PatrolMaintain maintain;
    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private EnterpriseInfoQuery constructionunitQuery;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;
    private static final String[] USER_ID_FIELDS = {SiteInspectionVO.CREATOR, InspectItemVO.PK_RECTIFICATIONER};
    private static final String[] USER_NAME_FIELDS = {SiteInspectionVO.CREATOR_NAME, InspectItemVO.RECTIFICATIONER_NAME};

    /**
     * 添加
     * @param vo
     * @return
     * @throws BusinessException
     */
    @Insert
    @PMApprove
    @RequestMapping(value = "/add")
    @ResponseBody
    public SiteInspectionVO add(@RequestBody SiteInspectionVO vo) throws BusinessException {
        vo.setCreator(RuntimeEnvironment.getLoginUserId());
        setUserInfo(new SiteInspectionVO[]{vo});
        return maintain.add(vo);
    }

    /**
     * 驳回后提交
     * @param siteInspectionVO
     * @return
     * @throws BusinessException
     */
    @PMApprove(trigger = "commit")
    @RequestMapping("/update")
    public SiteInspectionVO update(@RequestBody SiteInspectionVO siteInspectionVO) throws BusinessException {
        //直接调用Service层的update方法
        SiteInspectionVO update = maintain.update(siteInspectionVO);
        //为返回的对象追加创建者姓名这个属性，在这可加可不加，看需求
        userQuery.tranUserInfoField(new SiteInspectionVO[]{update}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return update;
    }

    private void setUserInfo(SiteInspectionVO[] siteInspectionVOS){
        if(siteInspectionVOS == null || siteInspectionVOS.length <= 0){
            return;
        }

        Set<String> userIdSet = new HashSet<>();

        for(SiteInspectionVO siteInspectionVO : siteInspectionVOS){

            // 整改人名称转换
            InspectItemVO[] inspectItemVOS = siteInspectionVO.getInspectitems();
            if(inspectItemVOS != null && inspectItemVOS.length > 0){
                for(InspectItemVO inspectItemVO : inspectItemVOS){
                    userIdSet.add(inspectItemVO.getPk_rectificationer());
                }
            }

            // 制单人名称转换
            if(StringUtils.isNotEmpty(siteInspectionVO.getCreator())){
                userIdSet.add(siteInspectionVO.getCreator());
            }
        }

        if(userIdSet.size() <= 0){
            return;
        }

        Map<String, String> userInfoMap = new HashMap<>();
        UserInfo[] userInfoArray = userQuery.queryUsers(userIdSet.toArray(new String[]{}));
        if(userInfoArray == null || userInfoArray.length <= 0){
            return;
        }
        for(UserInfo userInfo : userInfoArray){
            userInfoMap.put(userInfo.getUserId(), userInfo.getUserName());
        }

        for(SiteInspectionVO siteInspectionVO : siteInspectionVOS){
            siteInspectionVO.setCreator_name(userInfoMap.get(siteInspectionVO.getCreator()));

            // 整改人名称转换
            InspectItemVO[] inspectItemVOS = siteInspectionVO.getInspectitems();
            if(inspectItemVOS != null && inspectItemVOS.length > 0){
                for(InspectItemVO inspectItemVO : inspectItemVOS){
                    inspectItemVO.setRectificationer_name(userInfoMap.get(inspectItemVO.getPk_rectificationer()));
                }
            }
        }
    }
}
